<?php

namespace addons\mental\controller;

use app\admin\model\article\ArticleCollect;
use think\Db;

class Article extends Base
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['list','category_list','detail'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    private $model = null;
    private $articleCollectModel = null;
    private $articleCategoryModel = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\article\Article();
        $this->articleCollectModel = new \app\admin\model\article\ArticleCollect();
        $this->articleCategoryModel = new \app\admin\model\article\Category();

    }
    /**
     * 文章列表
     */
    public function list()
    {
        $pagesize = $this->request->param('pagesize', 10);
        $keywords = $this->request->param('keywords', '');
        $category_id = $this->request->param('category_id', 0);
        $where = [];
        $where['article.is_del'] = ['=',0];
        if ($keywords){
            $where['title'] = ['like', '%' . $keywords . '%'];
        }
        if ($category_id){
            $where['article_category_id'] = ['=',$category_id];
        }

        $list = $this->model
            ->with(['articlecategory'])
            ->where($where)
            ->paginate($pagesize);
        if ($list){
            foreach ($list as $key=>&$value){
                if ($this->auth->id){
                    $like = $this->articleCollectModel->where(['user_id'=>$this->auth->id,'main_id'=>$value['id']])->find();
                    $value['like'] = $like?1:0;
                }else{
                    $value['like'] = 0;
                }

            }
        }
        $this->success('成功', $list);
    }

    /*
     * 文章详情
     * */
    public function detail()
    {
        $id = $this->request->param('id', 0);
        $data = $this->model->where(['id'=>$id])->find();
        if (!$data){
            $this->error('文章不存在');
        }
        $this->success('成功', $data);
    }

    /*
     * 文章分类
     * */
    public function category_list()
    {
        $list = $this->articleCategoryModel->where(['is_del'=>0])->select();
        $this->success('成功', $list);
    }

    /*
     * 文章收藏
     * */
    public function collect()
    {
        $id = $this->request->param('id', 0);
        $like = $this->articleCollectModel->where(['user_id'=>$this->auth->id,'main_id'=>$id])->find();
        if ($like){
            $like->delete();
        }else{
            $this->articleCollectModel->save(['user_id'=>$this->auth->id,'main_id'=>$id]);
        }
        $this->success('成功', '');
    }

    /*
     * 文章收藏记录
     * */
    public function collect_list()
    {
        $pagesize = $this->request->param('pagesize', 10);
        $list = ArticleCollect::with(['article'])
            ->where(['user_id'=>$this->auth->id])
            ->order('id','desc')
            ->paginate($pagesize);
        if ($list){
            foreach ($list as $key=>&$value){
//                $value['article_category_id'] = $value['article']['article_category_id'];
                $value['article']['articlecategory'] = \app\admin\model\article\Category::where(['id'=>$value['article']['article_category_id']])->find();
            }

        }

        $this->success('成功', $list);
    }

}
